package com.yserp.project.erp.common.enums;

import com.yserp.common.utils.CacheUtils;
import com.yserp.common.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import static com.yserp.common.constant.Constants.USER_ORDER_CACHE;
import static com.yserp.common.constant.Constants.USER_ORDER_KEY;

/**
 * 订单类型
 * author:Carl
 * Date:2022/7/7
 * Time:14:56
 */
public enum OrderEnums {

    IN_ORDER("1", "IO", "入库单"),
    OUT_ORDER("2", "OO", "出库单"),
    PRODUCTION_ORDER("3", "TO", "排产单"),
    DELIVERY_ORDER("4", "DO", "发货单"),
    SALE_ORDER("5", "SO", "销售单"),
    PURCH_ORDER("7", "PO", "采购订单"),
    PAYMENT("8", "PM", "付款单"),
    BOM("9", "BM", "BOM规则"),
    RECEIVE_MONEY("10", "RM", "收款单"),



    //不存入cache
    CUSTOMER_DOCUMENT("6", "C", "客户档案"),

    ;

    private  String code;

    private  String message;

    private  String pref;

    private OrderEnums(String code,String pref, String message) {
        this.code = code;
        this.pref = pref;
        this.message = message;
    }

    public String getCode() {
        return  code;
    }

    public String getMessage() {
        return message;
    }

    public String getPref() {
        return pref;
    }

//    public static String getOrderNo(String pref) {
//        //时间
//        Date date = new Date();
//        SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMddHHmmssSSS"); // 获取当前时间
//        String dateStr = dmDate.format(date);
//        // 定义随机数
//        Random random = new Random();
//        // 随机数长度位数
//        int ran = random.nextInt(100);
//        //汇总
//        StringBuilder sb = new StringBuilder(pref);
//        sb.append(dateStr);
//        sb.append(ran);
//        return sb.toString();
//    }

    /**
     * 获取单号,加一
     * @param pref
     * @return
     */
    public static String getCacheOrderNo(String pref) {
        //时间规则
        SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMdd");
        //返回的单号
        StringBuilder sb = new StringBuilder(pref);
        sb.append(dmDate.format(new Date()));
        // 末尾编码
        String tail="001";
        String key=USER_ORDER_KEY+sb;
        Object value = CacheUtils.get(USER_ORDER_CACHE, key);
        if(null == value){
            CacheUtils.put(USER_ORDER_CACHE, key,0);
        }else{
            Integer number = Integer.valueOf(value.toString());
            number++;
            tail=getTail(number);
        }

        //汇总
        sb.append(tail);
        return sb.toString();
    }

    public static String getLowOrderNo(String pref) {
        //时间
        Date date = new Date();
        SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMdd");
        String dateStr = dmDate.format(date);
        // 定义随机数
        Random random = new Random();
        // 随机数长度位数
        int ran = random.nextInt(10000);
        //汇总
        StringBuilder sb = new StringBuilder(pref);
        sb.append(dateStr);
        sb.append(ran);
        return sb.toString();
    }

    /**
     * 单号缓存在cache，更新加一的数据
     * @param orderNo
     * @return
     */
    public static void putCacheOrderNo(String orderNo) {
        if(StringUtils.isBlank(orderNo)){
            return;
        }
        //汇总
        int length = orderNo.length();
        String head = orderNo.substring(0, length - 3);
        String tail = orderNo.substring(length - 3, length);
        Integer value = Integer.valueOf(tail);
        //写入缓存中
        String key=USER_ORDER_KEY+head;
        CacheUtils.put(USER_ORDER_CACHE, key, value);

    }

    /**
     * 单号缓存在cache，更新加一的数据
     * @param orderNo
     * @return
     */
    public static void putCacheOrderNo(String orderNo, int tail) {
        if(StringUtils.isBlank(orderNo)){
            return;
        }
        //汇总
        String head = orderNo.substring(0, orderNo.length() - 3);
        //写入缓存中
        String key=USER_ORDER_KEY+head;
        CacheUtils.put(USER_ORDER_CACHE, key, tail);

    }



    private static String getTail(Object number){
        return String.format("%03d", number);
    }


}
